package selcoursesys.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.springframework.orm.jpa.vendor.OpenJpaDialect;
import selcoursesys.dao.CourseDAO;
import selcoursesys.dao.impl.CourseDAOImpo;
import selcoursesys.entity.*;
import selcoursesys.service.CourseService;
import selcoursesys.service.SelectionService;
import selcoursesys.service.ShowCourseInfoService;
import selcoursesys.service.impl.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import java.lang.Object;

/**
 * Created by Lpbdz on 2017/4/9.
 */
public class SelectionAction extends ActionSupport implements ModelDriven<SelectionEntity>{
    private SelectionEntity selectionEntity=new SelectionEntity();
    private  List<CourseEntity> courseEntities;
    private HttpServletRequest request;
    private HttpSession session;
    public SelectionEntity getSelectionEntity() {
        return selectionEntity;
    }

    public void setSelectionEntity(SelectionEntity selectionEntity) {
        this.selectionEntity = selectionEntity;
    }

    public List<CourseEntity> getCourseEntities() {
        return courseEntities;
    }

    public void setCourseEntities(List<CourseEntity> courseEntities) {
        this.courseEntities = courseEntities;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpSession getSession() {
        return session;
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }

    public SelectionEntity getModel(){
        return selectionEntity;
    }
    /**
     *退课处理
     * 前段href传值，调用delsel处理，处理结束，返回studentInfo.jsp
     * 刷新课程信息
     */
    public String delSel() throws  Exception{
        HttpServletRequest request = ServletActionContext.getRequest();
        ShowCourseInfoService showCourseInfoService=new ShowCourseInfoServiceImpl();
        String courseid= request.getParameter("courseid");
        String stuid=request.getParameter("stuid");
        String ctype=request.getParameter("ctype");
        String term=request.getParameter("term");
        if(ctype.equals("e")) ctype="选修";
        else if(ctype.equals("f")) ctype="辅修";

        selectionEntity.setCourseid(courseid);
        selectionEntity.setStuid(stuid);
        SelectionService selectionService=new SelectionServiceImpl();
        //从选课表里删除数据
        if(selectionService.deleteSelection(selectionEntity)==true){
            CourseEntity courseEntity=new CourseEntity();
            CourseService courseService=new CourseServiceImpl();
            courseEntity.setCourseid(courseid);
            courseEntity=courseService.queryCourseId(courseEntity);
            courseEntity.setLnum(courseEntity.getLnum()-1);
            if(courseService.updateCourse(courseEntity)==true){
                showCourseInfoService.showCourseInfo(stuid,ctype,term);
                if(ctype.equals("选修"))  return "go_to_elev";
                if(ctype.equals("辅修"))  return "go_to_fuxiu";
            }else{
                return ERROR;
            }
        }else{
            return ERROR;
        }
        return ERROR;
    }
    /**
     *接受来自前端的课程信息链接，访问CoursDao返回处理数据
     * put到前端
     */
    public String showDetailCourse() throws Exception{
        List<CourseInfo> courseInfos=new LinkedList<CourseInfo>();
        HttpServletRequest request = ServletActionContext.getRequest();
        String courseId = request.getParameter("courseid");

        CourseEntity courseEntity = new CourseEntity();
        courseEntity.setCourseid(courseId);
        CourseService courseService=new CourseServiceImpl();
        courseEntity=courseService.queryCourseId(courseEntity);
        GetClassTime getClassTime=new GetClassTime();
        String cday=getClassTime.getClassTime(courseEntity.getCoursetime());
        String deccour="该课程为"+courseEntity.getAcademyEntity().getAname()+"开设,上课时间为第"+courseEntity.getBweek()+"周到第"+courseEntity.getEweek()+"周"+cday+ "已选人数为"+courseEntity.getLnum()+",可选人数为"+courseEntity.getCnum();

        courseInfos.add(new  CourseInfo(courseEntity.getCoursename(),courseEntity.getAcademyEntity().getAname(),courseEntity.getTeacherEntity().getTname(),
                courseEntity.getClasshour(),courseEntity.getCredit(),courseEntity.getExamine(),deccour));
        ActionContext.getContext().put("courseInfo",courseInfos);
        return "go_to_corinfo";
    }

    public String showCourseInfo() throws Exception{
        HttpServletRequest request = ServletActionContext.getRequest();
        String stuid=request.getParameter("stuid");
        String ctype=request.getParameter("ctype");
        String term=request.getParameter("term");
        if(ctype.equals("c")) ctype="必修";
        else if(ctype.equals("a")) ctype="ALL";
        else if(ctype.equals("e")) ctype="选修";
        else if(ctype.equals("f")) ctype="辅修";
        //System.out.println(stuid+term+ctype);
        //调用服务层处理
        ShowCourseInfoService showCourseInfoService=new ShowCourseInfoServiceImpl();
        if(showCourseInfoService.showCourseInfo(stuid,ctype,term).equals("success")){
            if(ctype.equals("必修")){
                return "go_to_comp";
            }else if(ctype.equals("ALL")){
                return "go_to_all";
            }else if(ctype.equals("选修")){
                return "go_to_elev";
            }else if(ctype.equals("辅修")){
                return "go_to_fuxiu";
            }
        }
        return "go_to_comp";
    }

    public String checkCourseTime() throws Exception{
        HttpServletRequest request = ServletActionContext.getRequest();
        String courseid= request.getParameter("courseid");
        String stuid=request.getParameter("stuid");
        String ctype=request.getParameter("ctype");
        String term=request.getParameter("term");
        if(ctype.equals("c")) ctype="必修";
        else if(ctype.equals("a")) ctype="ALL";
        else if(ctype.equals("e")) ctype="选修";
        else if(ctype.equals("f")) ctype="辅修";

        //查询要选课程的信息，判断是否存在时间冲突和人数问题
        SelectionService selectionService=new SelectionServiceImpl();
        if(selectionService.selectCourse(courseid,stuid,ctype,term)==false){
            if(ctype.equals("选修"))  return "go_to_elev";
            if(ctype.equals("辅修"))  return "go_to_fuxiu";
        }else{
            if(ctype.equals("选修"))  return "go_to_elev";
            if(ctype.equals("辅修"))  return "go_to_fuxiu";
        }
        return ERROR;
    }
}
